<?php
Start::main();

class Start
{
        public static function main()
        {
            $ins=new Start();
            $ins->run();
        }

        private function run()
        {
            if(Cnfg::load() == false)
            {
                return 1;
            }
            print_r(Cnfg::$log);
            print "\n";
            print_r(Cnfg::$database);
            print "\n";

            return 0;
        }
}

class Cnfg
{
    public static $log;
    public static $database;

    public static function load()
    {/*{{{*/
        $file=dirname(__FILE__)."/simple.xml";
        $root=simplexml_load_file($file);
        if($root === false)
        {
            printf("%s: load file(%s) fail\n", __METHOD__, $file);
            return false;
        }

        self::$database=array();
        foreach($root->children() as $name1=>$node1)
        {
            if($name1 == 'log')
            {
                self::$log=trim($node1);
            }
            else if($name1 == 'database')
            {/*{{{*/
                self::$database['connection']=array();
                foreach($node1->children() as $name2=>$node2)
                {
                    if($name2 == 'connection')
                    {
                        $node=array();
                        foreach($node2->children() as $k=>$v)
                        {
                            if(!in_array($k, array('id','user','password',
                                            'database','host')))
                            {
                                continue;
                            }
                            $v=trim($v);
                            if(empty($v))
                            {
                                printf("%s: %s.%s.%s invalid\n",
                                        __METHOD__, $name1, $name2,
                                        $k);
                                return false;
                            }
                            $node[$k]=$v;
                        }
                        if(!array_key_exists('id', $node) ||
                                !array_key_exists('user', $node) ||
                                !array_key_exists('password', $node) ||
                                !array_key_exists('database', $node))
                        {
                            printf("%s: %s.%s.id|user|password|database".
                                   " not found\n",
                                    __METHOD__, $name1, $name2);
                            return false;
                        }
                        if(!array_key_exists('host', $node))
                            $node['host']='localhost';
                        self::$database['connection'][$node['id']]=$node;
                    }
                }
            }/*}}}*/
        }

        return true;
    }/*}}}*/
}
?>
